home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Miscellaneous / CopyPaste 3.3.4 / CopyPaste Tools Sourcecode / XTND Import / XTNDFileIO.p < prev    next >
Encoding:
Text File  |  1997-02-26  |  12.8 KB  |  515 lines  |  [TEXT/PJMM]

  1.  
  2. UNIT XTNDFileIO;
  3.  
  4. INTERFACE
  5.  
  6.     USES
  7.         XTNDGlobals;
  8.  
  9. (*--------------------------- Routines in this file ----------------------------*)
  10.  
  11.     {$MAIN}
  12.                         
  13.     procedure main;        
  14.  
  15.  
  16. (* ========================================================================≠============≠============== *)
  17. IMPLEMENTATION
  18.  
  19.     CONST
  20. {QUICKDRAWSTYLES = ORD(bold) + ORD(italic) + ORD(underline) + ORD(outline) + ORD(shadow); }
  21.         QUICKDRAWSTYLES = 127;
  22.  
  23.  
  24.     PROCEDURE RGBFromXTND (VAR rgb: RGBColor; colorcode: INTEGER);
  25.     BEGIN
  26.         CASE colorcode OF
  27.             0:    { WHITE }
  28.                 BEGIN
  29.                 rgb.red := 65535;
  30.                 rgb.green := 65535;
  31.                 rgb.blue := 65535
  32.             END;
  33.             1:    { BLACK }
  34.                 BEGIN
  35.                 rgb.red := 0;
  36.                 rgb.green := 0;
  37.                 rgb.blue := 0
  38.             END;
  39.             2:    { RED }
  40.                 BEGIN
  41.                 rgb.red := 65535;
  42.                 rgb.green := 0;
  43.                 rgb.blue := 0
  44.             END;
  45.             3:    { GREEN }
  46.                 BEGIN
  47.                 rgb.red := 0;
  48.                 rgb.blue := 0;
  49.                 rgb.green := 65535
  50.             END;
  51.             4:    { BLUE }
  52.                 BEGIN
  53.                 rgb.red := 0;
  54.                 rgb.green := 0;
  55.                 rgb.blue := 65535
  56.             END;
  57.             5:    { CYAN }
  58.                 BEGIN
  59.                 rgb.red := 0;
  60.                 rgb.green := 65535;
  61.                 rgb.blue := 65535
  62.             END;
  63.             6:    { MAGENTA }
  64.                 BEGIN
  65.                 rgb.red := 65535;
  66.                 rgb.blue := 65535;
  67.                 rgb.green := 0
  68.             END;
  69.             7:    { YELLOW }
  70.                 BEGIN
  71.                 rgb.red := 65535;
  72.                 rgb.green := 65535;
  73.                 rgb.blue := 0
  74.             END
  75.         END
  76.     END;
  77.  
  78.  
  79.     FUNCTION GetStyleFrom (XTNDStyle: INTEGER): Style;
  80.         VAR
  81.             newStyle: Style;
  82.     BEGIN
  83.         newStyle := [];        { Plain }
  84.  
  85.         IF BAND(XTNDStyle, kQDBold) <> 0 THEN
  86.             newStyle := newStyle + [bold];
  87.  
  88.         IF BAND(XTNDStyle, kQDItalic) <> 0 THEN
  89.             newStyle := newStyle + [italic];
  90.  
  91.         IF BAND(XTNDStyle, kQDUnderline) <> 0 THEN
  92.             newStyle := newStyle + [underline];
  93.  
  94.         IF BAND(XTNDStyle, kQDOutline) <> 0 THEN
  95.             newStyle := newStyle + [outline];
  96.  
  97.         IF BAND(XTNDStyle, kQDShadow) <> 0 THEN
  98.             newStyle := newStyle + [shadow];
  99.  
  100.         GetStyleFrom := newStyle;
  101.     END;
  102.  
  103.  
  104.  
  105.  
  106. (* ========================================================================≠============≠============== *)
  107.     PROCEDURE ReadFile (pChosenOne: TransDescrPtr; theReply: SFReply);
  108.         VAR
  109.             
  110.             pm: pictMiscHdl;
  111.             importPB: ImportParmBlock;
  112.             hfsPB: ParamBlockRec;
  113.             Parafmt: ARRAY[0..8] OF Fixed;
  114.             Tabs: ARRAY[0..19] OF tabspec;
  115.             MinusOne: Point;
  116.             
  117.             Marker: ARRAY[0..9] OF Byte;
  118.             fnum, resfnum, fserr: INTEGER;
  119.             aPtr: IntegerPtr;
  120.             count, textrun: LONGINT;
  121.             newStyle: TextStyle;
  122.             Buffer, theNumber: Str255;
  123.             now: LONGINT;
  124.             dummy: OSErr;
  125.             
  126.             te: TEHandle;
  127.             destRect, viewRect: rect;
  128.     BEGIN
  129.         fnum := 0;
  130.         resfnum := 0;
  131.         textrun := 0;
  132.         now:=0;
  133.         
  134.         SetCursor(GetCursor(watchCursor)^^);
  135.         fserr := XTNDLoadTranslator(pChosenOne, gImportTranslator);
  136.         IF fserr <> noErr THEN BEGIN
  137.             EXIT(ReadFile);
  138.         END;
  139.         sysbeep(1);
  140.         MinusOne.v := -1;
  141.         MinusOne.h := -1;
  142.         destRect.left := 0;
  143.         destRect.right := 512;
  144.         destRect.top := 0;
  145.         destRect.bottom := 30000;
  146.  
  147.         setrect(viewRect,0,0,0,0);
  148.         
  149.         TE := TEStyleNew(destRect, viewRect);
  150.  
  151.         importPB.TextBuffer := @Buffer;
  152.         importPB.result := noErr;
  153.         importPB.TextLength := 0;
  154.         importPB.TxtFace := 0;    { Plain }
  155.         importPB.TxtSize := 0;
  156.         importPB.TxtFont := helvetica;
  157.         importPB.TxtColor := 0;
  158.         importPB.TxtJust := 0;    { Left }
  159.         importPB.ParaFmts := @Parafmt;
  160.         importPB.Tabs := @Tabs;
  161.         importPB.NumCols := 1;
  162.         importPB.CurrentStory := mainStory;
  163.         importPB.MiscData := 0;
  164.         importPB.StoryHeight := 0;
  165.         importPB.DecimalChar := '.';
  166.         importPB.AutoHyphenate := TRUE;
  167.         importPB.PrintRecord := NIL;
  168.         importPB.StartPageNum := 1;
  169.         importPB.StartFootnoteNum := 1;
  170.         Marker[0] := 0;
  171.         importPB.FootnoteText := @Marker;
  172.         importPB.RulerShowing := TRUE;
  173.         importPB.DoubleSided := FALSE;
  174.         importPB.TitlePage := FALSE;
  175.         importPB.Endnotes := FALSE;
  176.         importPB.ShowInvisibles := FALSE;
  177.         importPB.ShowPageGuides := TRUE;
  178.         importPB.ShowPictures := TRUE;
  179.         importPB.AutoFootnotes := TRUE;
  180.         importPB.PagePoint := MinusOne;
  181.         importPB.DatePoint := MinusOne;
  182.         importPB.TimePoint := MinusOne;
  183.         importPB.SmartQuotes := TRUE;
  184.         importPB.FractCharWidths := FALSE;
  185.         importPB.HRes := 72;
  186.         importPB.VRes := 72;
  187.         importPB.TheReply := theReply;
  188.         importPB.ThisTranslator := pChosenOne^;
  189.         IF OpenRFPerm(theReply.fName, theReply.vRefNum, fsRdPerm) = -1 THEN BEGIN
  190.             IF ResError <> eofErr THEN                { No resource fork found }
  191.                 BEGIN
  192.                 dummy := XTNDReleaseTranslator(pChosenOne);
  193.                 EXIT(ReadFile)
  194.             END;
  195.             UseResFile(pChosenOne^.ResRefNum);        { For translators expecting to be the current resource file }
  196.         END
  197.         ELSE        { If there is a resource fork for this file, read the resources }
  198.             BEGIN
  199.             resfnum := CurResFile;
  200.             importPB.RefNum := resfnum;
  201.             importPB.Directive := ImportGetResources;
  202.             XTNDCallTranslator(@importPB, gImportTranslator);
  203.             IF importPB.result <> noErr THEN BEGIN
  204.                 CloseResFile(resfnum);
  205.                 dummy := XTNDReleaseTranslator(pChosenOne);
  206.                 EXIT(ReadFile)
  207.             END
  208.         END;
  209.  
  210.     { Open the file read only }
  211.         fserr := 0;
  212.         hfsPB.ioNamePtr := @theReply.fName;
  213.         hfsPB.ioVRefNum := theReply.vRefNum;
  214.         hfsPB.ioVersNum := 1;
  215.         hfsPB.ioPermssn := fsRdPerm;
  216.         hfsPB.ioMisc := Ptr(0);
  217.         
  218.         fserr := PBOpenDFSync(@hfsPB);
  219.         IF fserr <> noErr THEN BEGIN
  220.             CloseResFile(resfnum);
  221.             dummy := XTNDReleaseTranslator(pChosenOne);
  222.             EXIT(ReadFile)
  223.         END;
  224.         sysbeep(1);
  225.         fnum := hfsPB.ioRefNum;
  226.         importPB.RefNum := hfsPB.ioRefNum;
  227.         importPB.Directive := ImportInitAll;
  228.         XTNDCallTranslator(@importPB, gImportTranslator);
  229.  
  230.     { After completing the initialization, check for an error.  If none, proceed. }
  231.         IF importPB.result <> noErr THEN BEGIN
  232.             CloseResFile(resfnum);
  233.             dummy := XTNDReleaseTranslator(pChosenOne);
  234.             EXIT(ReadFile)
  235.         END;
  236.  
  237.     { STAGE ONE - just read in the TEXT of the file.  Ignore pictures }
  238.  
  239.     { Set starting place to be the MAIN body of text. }
  240.         importPB.Directive := ImportInitMain;
  241.         importPB.CurrentStory := mainStory;
  242.         XTNDCallTranslator(@importPB, gImportTranslator);
  243.         IF importPB.result = noErr THEN BEGIN
  244.  
  245.             WHILE textrun < 30000 DO BEGIN
  246.                 importPB.Directive := ImportGetText;
  247.                 XTNDCallTranslator(@importPB, gImportTranslator);
  248.  
  249.                 fserr := importPB.result;
  250.                 count := importPB.TextLength;
  251.  
  252.                 IF (fserr <> noErr) OR ((importPB.Directive = ImportAcknowledge) AND (count <= 0)) THEN
  253.                     LEAVE;
  254.                 IF (count = 1) THEN BEGIN
  255.                     IF (ORD(Buffer[0]) < 32) THEN        { Is it a special character? }
  256.                         CASE ORD(Buffer[0]) OF
  257.                             2,    { Page Number }
  258.                             3,    { Footnote reference }
  259.                             5,    { Footnote reference }
  260.                             6,    { Merge Break Char }
  261.                             9,    { Tab }
  262.                             11,    { Column Break }
  263.                             12,    { Page Break }
  264.                             31:    { Discretionary Hyphen }
  265.                                 count := 0;
  266.  
  267.                             4:    { Picture }
  268.                             { We have to dispose of the picture, even if we don't use it. }
  269.                                 BEGIN
  270.                                 pm := pictMiscHdl(importPB.MiscData);
  271.                                 DisposeHandle(Handle(pm^^.ThePicture));
  272.                                 DisposeHandle(Handle(pm));
  273.                                 count := 0
  274.                             END;
  275.  
  276.                             21,    { Short Date }
  277.                             22,    { Abbrev Date }
  278.                             23,    { Long date }
  279.                             24,    { Abbrev + day Date }
  280.                             25:    { Long + day Date }
  281.                                 BEGIN
  282.                                 IF importPB.MiscData <> 0 THEN
  283.                                     IUDateString(importPB.MiscData, shortDate, theNumber)
  284.                                 ELSE
  285.                                     IUDateString(now, shortDate, theNumber);
  286.                                 count := ORD(theNumber[0]);
  287.                                 BlockMove(Ptr(ORD4(@theNumber) + 1), @Buffer, count);
  288.                             END;
  289.  
  290.                             26:    { Time }
  291.                                 BEGIN
  292.                                 IF importPB.MiscData <> 0 THEN
  293.                                     IUTimeString(importPB.MiscData, FALSE, theNumber)
  294.                                 ELSE
  295.                                     IUTimeString(now, FALSE, theNumber);
  296.                                 count := ORD(theNumber[0]);
  297.                                 BlockMove(Ptr(ORD4(@theNumber) + 1), @Buffer, count);
  298.                             END;
  299.  
  300.                             7:    { Hard Return }
  301.                                 Buffer[0] := CHR(13);
  302.                         END;
  303.                 END;
  304.  
  305.                 IF count <> 0 THEN BEGIN
  306.                     aPtr := IntegerPtr(@newStyle.tsFace);    { Fix a bug in text edit }
  307.                     aPtr^ := 0;
  308.  
  309.                     newStyle.tsFont := importPB.TxtFont;
  310.                     newStyle.tsFace := GetStyleFrom(importPB.TxtFace);
  311.                     newStyle.tsSize := importPB.TxtSize;
  312.                     RGBFromXTND(newStyle.tsColor, importPB.TxtColor);
  313.                     TESetStyle(doAll, newStyle, TRUE, te);
  314.  
  315.                 { Now add the number of characters to the text edit handle in this window }
  316.                     TEInsert(@Buffer, count, te);
  317.                     IF MemError <> noErr THEN
  318.                         LEAVE;
  319.  
  320.                     textrun := textrun + count;
  321.                 END;
  322.  
  323.  
  324.             END; {while}
  325.  
  326.             importPB.directive := importCloseMain;
  327.             XTNDCallTranslator(@importPB, gImportTranslator);
  328.             dummy := XTNDReleaseTranslator(pChosenOne);
  329.         END;
  330.  
  331.         importPB.directive := importCloseAll;
  332.         XTNDCallTranslator(@importPB, gImportTranslator);
  333.  
  334.         IF resfnum <> 0 THEN
  335.             CloseResFile(resfnum);
  336.         dummy := FSClose(fnum);
  337.         dummy := XTNDReleaseTranslator(pChosenOne);
  338.         TESetSelect(0, te^^.teLength, te);
  339.         IF ZeroScrap = noErr THEN BEGIN
  340.             TECopy(te);
  341.         END;
  342.     END;
  343.  
  344. (* ------------------------------------------------------------------------+------------+-------------- *)
  345.     FUNCTION ReadPlainTextFile (theReply: SFReply; VAR hTx: Handle): OSErr;
  346.  
  347.         LABEL
  348.             86;
  349.         VAR
  350.             err, dummy: OSErr;
  351.             myPB: ParamBlockRec;
  352.     BEGIN
  353.         SetCursor(GetCursor(watchCursor)^^);
  354.         hTx := NIL;
  355.     { open the text file… }
  356.         myPB.ioNamePtr := @theReply.fName;
  357.         myPB.ioVRefNum := theReply.vRefNum;
  358.         myPB.ioVersNum := 0;
  359.         myPB.ioPermssn := fsRdPerm;
  360.         myPB.ioMisc := NIL;
  361.         err := PBOpenDFSync(@myPB);
  362.         IF err <> noErr THEN BEGIN
  363.             ReadPlainTextFile := err;
  364.             EXIT(ReadPlainTextFile)
  365.         END;
  366.     { find out how much text in the file… }
  367.         err := PBGetEOFsync(@myPB);
  368.         IF err <> noErr THEN BEGIN
  369.             ReadPlainTextFile := err;
  370.             EXIT(ReadPlainTextFile)
  371.         END;
  372.     { get a buffer for the text… }
  373.         hTx := NewHandle(LONGINT(myPB.ioMisc));
  374.         IF hTx = NIL THEN BEGIN
  375.             GOTO 86
  376.         END;
  377.         MoveHHi(hTx);
  378.         HLock(hTx);
  379.     { read the file into the buffer… }
  380.  
  381.         if FSRead(myPB.ioRefnum, LONGINT(myPB.ioMisc), hTx^)= noerr then begin;
  382.             ;
  383.         END;
  384.         
  385.         IF ZeroScrap = noErr THEN BEGIN
  386.             err := PutScrap(gethandlesize(hTx), 'TEXT', hTx^);
  387.         END;
  388. 86:
  389.         IF hTx <> NIL THEN
  390.             DisposeHandle(hTx);
  391.         dummy := FSClose(myPB.ioRefnum);
  392.         ReadPlainTextFile := err;
  393.     END;
  394.  
  395.  
  396. (* ========================================================================≠============≠============== *)
  397.     PROCEDURE DoOpen;
  398.  
  399. {    If the XTND Library was successfully initialized its XTNDGetFile()}
  400. {routine is used to get the user’s document selection, otherwise the}
  401. {Standard File SFGetFile() routine is used. *)}
  402. (*    /04.19.91 m_o *)
  403.         VAR
  404.             getIt: BOOLEAN;
  405.             myReply: SFReply;
  406.             myXSFPB: SFParamBlock;
  407.             myPrompt, myBTitle: Str255;
  408.             where: Point;
  409.             myTypes: SFTypeList;
  410.             Docte: Handle;
  411.             err: OSErr;
  412.     BEGIN
  413.         IF gXTNDAvail = TRUE THEN BEGIN
  414.             myXSFPB.AllowFlags := allowText;
  415.             myXSFPB.NumStandard := kNativeTypes;
  416.             myXSFPB.Standard := @gMyFileType;
  417.             myXSFPB.ioResult := 0;
  418.             myXSFPB.FileReply := @myReply;
  419.             myXSFPB.XTNDDlogHook := NIL;        { XTNDDlgHookProcPtr(MyDlg); }
  420.             myXSFPB.CurrentMenuItem := Load_stored;
  421.             myXSFPB.Where.v := 0;
  422.             myXSFPB.Where.h := 0;
  423.             myPrompt := 'Select a file to open';
  424.             myXSFPB.Prompt := @myPrompt;
  425.             myBTitle := 'Open';
  426.             myXSFPB.ButtonTitle := @myBTitle;
  427.             myXSFPB.DialogID := 0;
  428.             myXSFPB.SFFilterProc := NIL;
  429.             myXSFPB.ShowAllFiles := FALSE;
  430.             myXSFPB.useMyTransList := FALSE;
  431.             myXSFPB.myFileFilter := NIL;
  432.             myXSFPB.Unused := 0;
  433.             myReply.good := TRUE;
  434.             getIt := XTNDGetFile(@myXSFPB);
  435.             Load_stored := myXSFPB.CurrentMenuItem
  436.         END
  437.         ELSE BEGIN
  438.             where.v := $40;
  439.             where.h := $40;
  440.             myTypes[1] := 'TEXT';
  441.             SFGetFile(where, '', NIL, 1, @myTypes, NIL, myReply);
  442.  
  443.             getIt := myReply.good
  444.         END;
  445.         IF getIt = TRUE THEN BEGIN
  446.             IF (gXTNDAvail = TRUE) & (myXSFPB.chosenTranslator > myXSFPB.NumStandard) THEN
  447.                 ReadFile(myXSFPB.theChosenTranslator, myReply)    { Read the file in using XTND. }
  448.             ELSE BEGIN
  449.                 err := ReadPlainTextFile(myReply, docTE)
  450.             END;
  451.         END;
  452.     END;
  453.     
  454.     
  455.     
  456.     
  457. PROCEDURE Initialize;
  458.         CONST
  459.         { ———— Defines for XTND resources ———— }
  460.             clarisNames = 25003;    { Claris names STR# resource }
  461.             clarisFolder = 1;
  462.             xtndNames = 25004;    { XTND names STR# resource }
  463.             clarisTranslators = 1;
  464.             xtndSystem = 2;
  465.  
  466.         VAR
  467.             XTNDSystemName, ClarisFolderName: Str255;
  468.             gxtErr: OSErr;
  469.     BEGIN { Initialize }
  470.  
  471.         InitCursor;
  472.  
  473.  
  474.     { one-time initialization of the XTND Library… }
  475.         GetIndString(XTNDSystemName, xtndNames, xtndSystem);
  476.         GetIndString(ClarisFolderName, clarisNames, clarisFolder);
  477.  
  478.         gxtErr := XTNDInitTranslators(kTransVersion, XTNDSystemName, ClarisFolderName);
  479.         IF gxtErr <> noErr THEN BEGIN
  480.             gXTNDAvail := FALSE;
  481.         END
  482.         ELSE BEGIN
  483.             gXTNDAvail := TRUE;
  484.             gMyFileType[1].Version := 2;
  485.             gMyFileType[1].TranslatorType := 'FLTI';
  486.             gMyFileType[1].CodeResID := 0;
  487.             gMyFileType[1].FDIFResID := -1;
  488.             gMyFileType[1].NumVersBytes := 0;
  489.             gMyFileType[1].PathLength := 0;
  490.             gMyFileType[1].Flags := 0;
  491.             gMyFileType[1].NumMatches := 1;
  492.             gMyFileType[1].Matches[0].DocCreator := 'XTND';
  493.             gMyFileType[1].Matches[0].DocType := 'TEXT';
  494.             gMyFileType[1].Matches[0].ExactMatch := FALSE;
  495.             gMyFileType[1].Matches[0].creatorAndTypeMask := 0;
  496.             gMyFileType[1].Name := 'Text';
  497.             Load_stored := 1;
  498.             Save_stored := 1;
  499.         END;
  500.     END; {Initialize}
  501.  
  502.  
  503. PROCEDURE main;
  504.     var
  505.         oldA4: LongInt;
  506.  
  507.     BEGIN { main program }
  508.         oldA4 := SetCurrentA4;
  509.         Initialize;                    { initialize the program }
  510.         DoOpen;
  511.         oldA4 := SetA4(oldA4);
  512.     END;
  513.  
  514.  
  515. END.